package com.tjetc.service.impl;

import com.tjetc.dao.LogisticsPartnerDao;
import com.tjetc.entity.FriendLinks;
import com.tjetc.entity.LogisticsPartner;
import com.tjetc.service.LogisticsPartnerService;
import com.tjetc.util.Pager;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * (LogisticsPartner)表服务实现类
 *
 * @author makejava
 * @since 2025-09-19 10:34:25
 */
@Service("logisticsPartnerService")
public class LogisticsPartnerServiceImpl implements LogisticsPartnerService {
    @Resource
    private LogisticsPartnerDao logisticsPartnerDao;

    @Override
    public Object queryByPage(String name,Integer pageNum,Integer pageSize) {
        //查询总记录数
        Integer recordCount = logisticsPartnerDao.count(name);
        //构建分页信息对象
        Pager pager = new Pager(pageNum, pageSize, recordCount);
        //根据pager查询需要的数据
        List<LogisticsPartner> list = logisticsPartnerDao.query(name,pager.getStart(), pageSize);
        //将分页信息和查询到的数据一起返回到前端
        HashMap<String,Object> map = new HashMap<>();
        map.put("pager",pager);
        map.put("list",list);
        return map;
    }

    @Override
    public LogisticsPartner findById(Long id){
        LogisticsPartner logisticsPartner= logisticsPartnerDao.findById(id);
        return logisticsPartner;
    }

    @Override
    public void updateById(LogisticsPartner logisticsPartner) {
        this.logisticsPartnerDao.updateById(logisticsPartner);
    }

    @Override
    public void insert(LogisticsPartner logisticsPartner) {
        this.logisticsPartnerDao.insert(logisticsPartner);
    }

    @Override
    public void deleteById(Long id) {
        this.logisticsPartnerDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 任何异常都回滚，保证数据一致性
    public void setDefaultPartner(Long partnerId) {
        // 步骤1：先重置所有记录的 is_default 为 0
        logisticsPartnerDao.resetAllDefault();
        // 步骤2：再将指定 partnerId 的 is_default 设为 1
        logisticsPartnerDao.setDefaultByPartnerId(partnerId);
        // 无需手动提交：事务会自动提交（若两步都成功）
    }


}
